home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / jazlib.arc / JZSEARCH.DMO < prev    next >
Text File  |  1988-12-18  |  10KB  |  290 lines

  1. /*
  2. ┌────────────────────────────────────────────────────────────────────────────┐
  3. │ jzsearch                                     │
  4. │ Search a given file spec for a given string.                     │
  5. │ Synopsis:                                     │
  6. │                                         │
  7. │    Usage JZSEARCH [-uwcr] [filespec] [search string\\replace string]         │
  8. │    Options are as follows:\n");                                            │
  9. │    -u      Uppercase (Ignores case on search)                 │
  10. │    -c      Display files on console                     │
  11. │    -w      Whole words only. "MEM" won't be found in "MEMORY"          │
  12. │    -r      Replace string if found. Syntax for this is:"               │
  13. │    -p      Prompt for change before replacing                 │
  14. └────────────────────────────────────────────────────────────────────────────┘
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <jzdirect.h>
  19. #define UPPER    1        /* bit flag for upper case */
  20. #define ALL    2        /* bit flag for search all */
  21. #define CONSOLE 4        /* bit flag for console out */
  22. #define REPLACE 8        /* bit flag for replace     */
  23. #define WORD    16        /* bit flag for WORD search */
  24. #define PROMPT    32        /* bit flag for prompting change */
  25. #define PUNCT    "!@#$%^&*()_+|-=\\~` <>?,./:\";'[]{}\000\009\015\012"
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char **argv;
  30. {
  31.  
  32.   FILE *fd;            /* file variable */
  33.   FILE *TMP;            /* temporary file variable */
  34.   char wstr[256];        /* string to read info into */
  35.   char wtemp[256];        /* temp string for loading */
  36.   char worig[256];        /* original string for search */
  37.   char wsearch[256];        /* string to search on */
  38.   char wreplace[256];        /* string to replace wsearch with */
  39.   char wpath[65];        /* hold the path string */
  40.   char wfspec[65];        /* path + file name */
  41.   char wunq[65];        /* unique file name */
  42.   char *wch;            /* eventual pointer to wstr */
  43.   int werr;            /* error result of directory search */
  44.   TDIR wdir;            /* disk transfer buffer for dir search */
  45.   int windex;            /* position of last "\" in file spec */
  46.   int w,wok;            /* scratch integer */
  47.   int OPTION = 0;        /* set option flags */
  48.   int woffset;            /* offset of command line arguments */
  49.   char *wpos,*jzstrpos();    /* position of possible string        */
  50.   int wlen;            /* hold length of a string */
  51.  
  52.   printf("\nJZSEARCH (C) JazSoft by Jack A. Zucker 301-794-5950 | CIS: 75766,1336");
  53.  
  54.   if (argc < 3) {
  55.     printf("\nUsage JZSEARCH [-upwcr] [filespec] [search string\\replace string]");
  56.     printf("\nOptions are as follows:\n");
  57.     printf("\n-u          Uppercase (Ignores case on search)");
  58.     printf("\n-c          Display files on console");
  59.     printf("\n-w          Whole words only. \"MEM\" won't be found in \"MEMORY\"");
  60.     printf("\n-p          Prompt for change before replacing");
  61.     printf("\n-r          Replace string if found. Syntax for this is:");
  62.     printf("\n            jzsearch -r *.pas begin\\BEGIN");
  63.     printf("\n            which would replace all \"begin\"s with \"BEGIN\"s");
  64.     printf("\n            The \"\\\" is the delimiter, use \"\\\\\" to insert a");
  65.     printf("\n            slash into the search or replace spec.");
  66.     printf("\nNote that for straight searches, options are not required.");
  67.     printf("\nFor example jzsearch *.pas begin\n");
  68.     exit(1);
  69.   }
  70.  
  71.   if (getswitch(argv[1],&OPTION))
  72.     woffset = 2;    /* allow for the options argument */
  73.   else
  74.     woffset = 1;    /* no options argument */
  75.  
  76.   strcpy(wtemp, argv[woffset+1]);     /* get search info into wsearch */
  77.  
  78.   /**
  79.    ** Allow searches on words seperated by spaces by re - creating
  80.    ** command line. woffset + 2 will yield the second word in the
  81.    ** search specification if there is one.
  82.    **/
  83.  
  84.   for (w = woffset + 2 ; *argv[w] ; w ++) {
  85.     strcat(wtemp," ");                /* add a space */
  86.     strcat(wtemp,argv[w]);          /* add new arg */
  87.   }
  88.  
  89.   /**
  90.    ** If we are in replace mode, look for the "\" character as the delimiter
  91.    ** between the search string and the replace string. If we encounter 2
  92.    ** "\" chars then we insert one "\" in the string !
  93.    **/
  94.  
  95.   *wsearch = 0;             /* init search and replace strings */
  96.   *wreplace = 0;
  97.  
  98.   if (OPTION & REPLACE) {
  99.     strcpy(wunq,"\\");                 /* create unique file in root */
  100.     jzunqfil(w,wunq,0);            /* get unique file name         */
  101.     jzclsfil(w);               /* close it, we only need its name */
  102.     for (w = 0 ; w < strlen(wtemp) ; w ++)
  103.       if (wtemp[w] == '\\')
  104.     if (wtemp[w+1] != '\\') {
  105.       parse(wtemp+w+1,wreplace);    /* get replace part of string */
  106.       wtemp[w] = 0;         /* null terminate string */
  107.       parse(wtemp,wsearch);     /* get search part of string */
  108.       break;
  109.     }
  110.     else
  111.       w ++;             /* get past double "\"  */
  112.  
  113.     if (*wsearch == 0) {        /* check for null search string */
  114.       printf("\nNothing to search on. Aborting...");
  115.       if (OPTION & REPLACE)
  116.     unlink(wunq);            /* delete unique file here */
  117.       exit(1);
  118.     }
  119.   }
  120.   else
  121.     strcpy(wsearch,wtemp);        /* otherwise only one string */
  122.  
  123.   if (OPTION & UPPER)
  124.     strupr(wsearch);              /* convert to upper case */
  125.  
  126.   /**
  127.    ** Check and see whether a path was specified or not
  128.    **/
  129.  
  130.   if ((windex = rindex(argv[woffset],'\\')) != -1) {
  131.     strncpy(wpath,argv[woffset],windex+1);
  132.     *(wpath+windex+1) = 0;
  133.   }
  134.   else *wpath = 0;
  135.  
  136.   werr = jzfndfst(argv[woffset],32,&wdir);    /* find first matching file */
  137.   if ( ! werr )
  138.     do {
  139.  
  140.       strcpy(wfspec,wpath);        /* get path */
  141.       strcat(wfspec,wdir.name);     /* concatenate file name */
  142.  
  143.       fd = fopen(wfspec,"r");                /* open source file for read */
  144.  
  145.       if (OPTION & REPLACE)
  146.     TMP = fopen(wunq,"w");               /* open destin for write     */
  147.  
  148.       wlen = strlen(wsearch);             /* get length of search string */
  149.  
  150.       while (wch = fgets(wstr,255,fd)) {/* read a line from the file */
  151.     wok = 1;               /* set flag true */
  152.     strcpy(worig,wstr);           /* save original copy of string */
  153.     if (OPTION & UPPER)
  154.       strupr(wstr);            /* convert to upper case     */
  155.  
  156.     /**
  157.      ** This if statement checks for the existance of
  158.      ** the search string in the buffer of chars most
  159.      ** recently read from the file.
  160.      **/
  161.  
  162.     if (wpos = jzstrpos(wsearch,wstr)) {
  163.  
  164.       /**
  165.        ** If OPTION & WORD, the first character before and after
  166.        ** the sub - string must either be punctuation characters
  167.        ** or the first char or last character of the line.
  168.        **/
  169.  
  170.       if (OPTION & WORD)
  171.         wok = ((wpos ==  wstr) || (index(PUNCT,*(wpos-1)) != -1)) &&
  172.           (index(PUNCT,*(wpos+wlen)) != -1);
  173.  
  174.       /**
  175.        ** If we are in replace mode and prompt mode
  176.        ** we will prompt the user before changing the
  177.        ** string in question.
  178.        **/
  179.  
  180.       if (wok && (OPTION & PROMPT) && (OPTION & REPLACE)) {
  181.         printf("\nFile name     : %s",wfspec);
  182.         printf("\nSearch string : %s",wsearch);
  183.         printf("\nFull String   : %s",worig);
  184.         printf("Replace String: %s",wreplace);
  185.         printf("\nEnter \"Y\" to confirm change: ");
  186.         w = getch();
  187.         printf("\n");
  188.         wok = (toupper(w) == 'Y');
  189.       }
  190.  
  191.       if (wok) {
  192.         if (OPTION & REPLACE) {        /* are we in replace mode ? */
  193.           w = (int) wpos - (int) wstr;    /* convert pointer to index */
  194.           jzdltstr(worig,w,wlen);        /* delete the old string */
  195.           jzinsstr(worig,wreplace,w);    /* insert the new string */
  196.         }
  197.         if ( ! (OPTION & CONSOLE) )
  198.           printf("\n\"%s\" found in %s\n%s",wsearch,wdir.name,worig);
  199.         if (! (OPTION & ALL))    /* don't continue to search this file */
  200.           break;
  201.       }
  202.     }
  203.     if (OPTION & CONSOLE)            /* print to the console? */
  204.       if (OPTION & REPLACE)
  205.         fputs(worig,TMP);            /* write to temp file to */
  206.       else
  207.         printf("%s",worig);
  208.       }
  209.       fclose(fd);
  210.       if (OPTION & REPLACE) {            /* are we in replace mode? */
  211.     fclose(TMP);
  212.     unlink(wfspec);             /* delete original */
  213.     rename(wfspec,wunq);            /* rename unq to orig */
  214.       }
  215.       werr = jzfndnxt(&wdir);            /* get next file spec */
  216.     } while ( ! werr );
  217.   else
  218.     printf("\nNo matching files...");
  219. }
  220.  
  221.   /**
  222.    ** Evaluate switches set by the user and *OR* the appropriate bits
  223.    ** to *ON* .
  224.    ** options are as follows:
  225.    ** UPPER   1
  226.    ** ALL     2
  227.    ** CONSOLE 4
  228.    ** REPLACE 8
  229.    ** WORD    16
  230.    ** PROMPT  32
  231.    **/
  232.  
  233. getswitch(fstr,woption)
  234. char *fstr;
  235. int *woption;
  236. {
  237.   char ch;
  238.  
  239.   if (*fstr != '-' && *fstr != '/') return(0);
  240.  
  241.   while (ch = *++fstr)
  242.     switch(toupper(ch)) {
  243.       case 'U' : (*woption) |= UPPER ;
  244.          break;
  245.       case 'A' : (*woption) |= ALL ;
  246.          break;
  247.       case 'C' : (*woption) |= (ALL + CONSOLE);
  248.          break;
  249.       case 'R' : (*woption) |= (ALL + CONSOLE + REPLACE);
  250.          break;
  251.       case 'W' : (*woption) |= WORD;
  252.          break;
  253.       case 'P' : (*woption) |= PROMPT;
  254.          break;
  255.     }
  256.  
  257.   return(1);
  258. }
  259.  
  260.   /**
  261.    ** Parse the string looking for double "\"
  262.    ** chars. If we encounter two, then we insert
  263.    ** one into the string. Later, the routine
  264.    ** will treat one "\" as the escape char.
  265.    **/
  266.  
  267.  
  268. parse(fsource,fdestin)
  269. char *fsource,*fdestin;
  270. {
  271.  
  272.   int w,windex;
  273.  
  274.   *fdestin = 0;             /* create null string */
  275.   windex = 0 ;                /* set starting index */
  276.  
  277.   for (w = 0 ; w < strlen(fsource) ; w ++) {
  278.     if (fsource[w] == '\\')
  279.       if (fsource[w+1] == '\\') {
  280.     fdestin[windex] = '\\';       /* put 1 "\" in string */
  281.     w ++;                  /* point past dummy "\" */
  282.       }
  283.       else ;
  284.     else
  285.       fdestin[windex] = fsource[w];
  286.     windex ++;                  /* increment index position */
  287.   }
  288.   fdestin[windex] = 0;              /* put terminator in search string */
  289. }
  290.